
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const getFormatDate = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return ('' + year).padStart(4, '0') + '-' + ('' + month).padStart(2, '0') + '-' + ('' + day).padStart(2, '0')
}

const getFormatToday = () => {
  let timestamp = new Date().getTime()
  let d = new Date(timestamp)
  return getFormatDate(d)
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

//字符串实际长度，考虑中文
const stringLength = function (s) {
  var len = 0, code = 0;
  for (var i = 0; i < s.length; i++) {
      code = s.charCodeAt(i);
      if (code >= 0 && code <= 127) {
          len += 1;
      } else {
          len += 2;
      }
  }
  return len;
}

//截取字符串一定长度，超出长度用...代替(实际长度始终不超过long,考虑中文,一个中文算两个长度)。
const cutString = function (s, long) {
  var charLen = [];
  var len = 0, code = 0;
  for (var i = 0; i < s.length; i++) {
      code = s.charCodeAt(i);
      if (code >= 0 && code <= 127) {
          //英文
          len += 1;
          charLen.push(1);
      } else {
          //中文
          len += 2;
          charLen.push(2);
      }

      if (len > long) {
          //超出长度，用...
          var cl = charLen.pop();
          if (len - cl > long - 3) {
              var cl2 = charLen.pop();
              if (len - cl - cl2 > long - 3) {
                  var cl3 = charLen.pop();
                  if (len - cl - cl2 - cl3 > long - 3) {
                      charLen.pop();
                  }
              }
          }

          return s.substring(0, charLen.length) + '...';
      }
  }
  return s;
}

const deepCopy = function(o) {
  if (o === null) return null;

  if (typeof o == "object"){
      if (Array.isArray(o)){  //数组
          var n = [];
          for (var i = 0; i < o.length; ++i) {
              n[i] = deepCopy(o[i]);
          }
          return n;

      }else{   //字典
          var n = {}
          for (var i in o) {
              n[i] = deepCopy(o[i]);
          }
          return n;

      }
  } else {
      return o;
  }
}

const NumberChnArr = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
//数字转文字, 如果失败返回空字符串
const numToChn = function(num) {
  let result = ''
  let num_str = '' + num
  for (let i = 0; i < num_str.length; i++) {
    let t = parseInt(num_str.substr(i, 1))
    if (isNaN(t)) {
      return ''
    }
    let chn = NumberChnArr[t]
    if (chn === undefined) {
      return ''
    }
    result += chn
  }
  return result
}

const getNameFromUrl = function (url) {
  let filename = ''
  let parts = url.split('/')
  if (parts.length > 0) {
    filename = parts[parts.length - 1]
  }
  return filename
}

//排行转中文，异常返回空字符串
const priority2Chn = function(priority) {
  if (priority < 1 || priority > 20) {
    return ''
  }
  if (1 == priority) {
    return '老大'
  }
  if (10 == priority) {
    return '老十'
  }
  if (20 == priority) {
    return '老二十'
  }
  let chn_map = ['', '一', '二', '三', '四', '五', '六', '七', '八', '九']
  if (priority < 10) {
    return '老' + chn_map[priority]
  } else {
    //10到20之间
    return '老十' + chn_map[priority - 10]
  }
}

//排行转数字, 异常返回0
const priority2Num = function(priorityChn) {
  priorityChn = priorityChn.substr(1)  //去除老字
  if (priorityChn == '大') {
    return 1
  }
  if (priorityChn == '十') {
    return 10
  }
  if (priorityChn == '二十') {
    return 20
  }

  let chn_map = ['', '一', '二', '三', '四', '五', '六', '七', '八', '九']
  if (priorityChn.length == 1) {
    for (let i = 1; i <= 9; i++) {
      if (chn_map[i] == priorityChn) {
        return i
      }
    }
    return 0  //异常
  } else if (priorityChn.length == 2) {
    if (priorityChn[0] != '十') {
      return 0
    }
    priorityChn = priorityChn.substr(1)  //去除十字
    for (let i = 1; i <= 9; i++) {
      if (chn_map[i] == priorityChn) {
        return 10 + i
      }
    }
    return 0  //异常

  } else {
    return 0  //异常
  }
}

module.exports = {
  getFormatDate,
  getFormatToday,
  formatTime,
  stringLength,
  cutString,
  deepCopy,
  numToChn,
  getNameFromUrl,
  priority2Chn,
  priority2Num
}
